package com.arron.algorithm.leetcodetop100.数组;

import java.util.Arrays;

public class 颜色分类 {

    public void sortColors(int[] nums) {
        //指向 nums为0的数
        int p1 =0;
        //指向nums为1的数
        int p2 =0;

        //指向 nums 为 2的数
        int p3 = nums.length-1;


        while (p2<=p3){
            if (nums[p2] == 0){
                swap(nums,p1,p2);
                p1++;
                p2++;
                continue;

            }

            if (nums[p2] == 1){
               p2++;
               continue;
            }
            if (nums[p2] == 2){
                swap(nums,p2,p3);
                p3--;
            }

        }
    }

    private void swap(int[] nums, int p1, int p2) {
        int temp = nums[p1];
        nums[p1] = nums[p2];
        nums[p2] = temp;
    }


    public static void main(String[] args) {

        颜色分类 sort = new 颜色分类();
        int[] nums={2,0,2,1,1,0};

        sort.sortColors(nums);

        System.out.println(Arrays.toString(nums));
    }

}
